package com.abc.files;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.abc.model.PersonYearEffort;
import com.abc.model.Project;
import com.abc.model.ProjectYear;
import com.abc.model.ReportRepository;

public class LoadRepo {


	public boolean addFilesToRepo(ReportRepository repo, String directory) {
		boolean succeed = false;
		FileFinder fFinder = new FileFinder(directory);
		List<FileRecord> filesList = fFinder.getRecords();
		
		for(FileRecord fRecord : filesList) {
			succeed = true;
			readXls(fRecord.getName(), fRecord.getYear(), fRecord.getMonth(), repo, fRecord.getFile());
		}
		
		return succeed;
	}
	
	protected void readXls(
			String devName, String fileYear, String fileMonth, ReportRepository repos, File theFile
			) {

		PersonYearEffort devYear = new PersonYearEffort(devName, fileYear);

		try {
			FileInputStream file = new FileInputStream(theFile);
			
			XSSFWorkbook workbook = null;
			try {
				workbook = new XSSFWorkbook(file);
			} catch (org.apache.poi.POIXMLException e) {
				System.err.println("problem wirh file : " + theFile + " - " + e.getMessage());
				e.printStackTrace();
				return;
			}
			// Get first/desired sheet from the workbook

			int sheetsNum = workbook.getNumberOfSheets();
			for(int sheetNumber=0; sheetNumber < sheetsNum ; sheetNumber++) {

				XSSFSheet sheet = workbook.getSheetAt(sheetNumber);

				String projName = workbook.getSheetName(sheetNumber);

				Project projectFromSheet = new Project(projName);

				Iterator<Row> rowIterator = sheet.iterator();
				rowIterator.next(); // omit header

				double[] monthEfforts = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
				Date workDate = Calendar.getInstance().getTime(); 

				int maxRows = 1000;
				int currentRow=1;
				while (rowIterator.hasNext() && currentRow < maxRows) {
					Row row = rowIterator.next();
					// For each row, iterate through all the columns
					Iterator<Cell> cellIterator = row.cellIterator();
					if (row.getCell(0)!= null) {
						workDate = row.getCell(0).getDateCellValue();
						currentRow++;
					} else {
						continue;
					}
					if (row.getCell(1) != null){
						//task name skipped : row.getCell(1).getStringCellValue();
						currentRow++;
					}


					if (row.getCell(2) != null){
						double hours = row.getCell(2).getNumericCellValue();
						//					System.out.println("hours : " + hours +" / " + workDate.getMonth());
						monthEfforts[workDate.getMonth()] += hours;  
						currentRow++;
					}
				}

				for(int i=0; i < monthEfforts.length; i++) {
					devYear.addEffort(i+1, monthEfforts[i]);
				}

				ProjectYear pYear = new ProjectYear();
				pYear.addPersonEfforts(devYear);
				projectFromSheet.addProjectYear(pYear);
				repos.addProject(projectFromSheet);
			}
			//------------------------------- sheets

			file.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}



}
